import config from "../../configGame/configGame"

Component({
  mixins: [],
  data: {
    w:600,
    h:600,
    url:'https://img.alicdn.com/imgextra/i1/4161056916/O1CN0195Ddp720xZqJxFuRB_!!4161056916.png',   //more 
    difficulty:3,
    row:3,
    colum:4,
    gameData:[],
    bili:0,
    showImg:false,

    opponent:[],

    mineData:{},
    enemyData:'',
    downTime:30,
    step:100,
		bgColor:"#58FA58",
		
		// bg:{
		// 	url:'https://img.alicdn.com/imgextra/i4/4161056916/O1CN01nSSczZ20xZql2BbY0_!!4161056916.png'
		// },

		// tagImg:[
		// 	{
		// 		url:'https://img.alicdn.com/imgextra/i2/4161056916/O1CN01ZODAva20xZqnt39AV_!!4161056916.png'
		// 	},
		// 	{
		// 		url:'https://img.alicdn.com/imgextra/i2/4161056916/O1CN01DYtY6k20xZqj9mJWJ_!!4161056916.png'
		// 	}
		// ],

		// stepImgBg:{
		// 	url:'https://img.alicdn.com/imgextra/i4/4161056916/O1CN01qIJvYL20xZqfQFgVc_!!4161056916.png'
		// },
		// stepImgTg:{
		// 	url:'https://img.alicdn.com/imgextra/i2/4161056916/O1CN01w1Lwp820xZql3VExw_!!4161056916.png'
		// },

		// msgIcon:{
		// 	url:'https://img.alicdn.com/imgextra/i4/4161056916/O1CN01G23BRX20xZqkN110w_!!4161056916.png'
		// },

		saoHua:[
			{
				txt:'666'
			},
			{
				txt:'等等我～'
			},
			{
				txt:'你好棒'
			},
			{
				txt:'So Esasy'
			}
		],

		msg:'',
		showEnMsg:false,

		showSendMsg:false,

		showMineMsg:false,

		locaDataRandom:[
			{
				x:10,
				y:1000
			},

			{
				x:85,
				y:1000
			},

			{
				x:160,
				y:1000
			},

			
			{
				x:235,
				y:1000
			}
		]
  },
  onInit(){

		// this.setData({
		// 	url:this.$page.data.navBar[this.$page.data.currentIndex].url
		// })



		this.jieliuArr = []

		this.initData();

		this.$page.gamePage = this;

		this.setData({
      ...getApp().pifu.gamePage,
			isVip:getApp().isVip
		})
		
		// console.info('游戏时间====================>',this.props.gameTime)

		setTimeout(() => {
			this.setData({
				showImg:false
			})

			let time = this.data.downTime;
			let step = 100;
			let bili = 10 / this.data.downTime

			this.gameTime = setInterval(() => {

				
				time -= 1;

				if(time < 0){
					clearInterval(this.gameTime);
					return
				}

				this.setData({
					downTime:time,
					step:step
				})

			},1000)

			this.stepTime = setInterval(() => {
				
				if(step <= 0){
					clearInterval(this.stepTime);
					return
				}
				

				this.setData({
					step:step
				})
				

				step = step - bili < 0 ? 0 : step - bili;


			},100)

		}, 2000);
    
    
  },
  props: {},
  didMount() {

		

	},
  didUpdate() {},
  didUnmount() {},
  methods: {


		msg(o){


			if(this.data.showEnMsg)return
			
			this.setData({
				msg:o,
				showEnMsg:true,
				showMineMsg:false
			})

			clearTimeout(this.msgSet)

			this.msgSet = setTimeout(() => {
				this.setData({
					msg:'',
					showEnMsg:false
				})
			}, 3100);

		},

		mineMsg(o){

			if(this.data.showMineMsg)return
			
			this.setData({
				msg:o,
				showMineMsg:true,
				showEnMsg:false
			})

			clearTimeout(this.msgSet)

			this.msgSet = setTimeout(() => {
				this.setData({
					msg:'',
					showMineMsg:false
				})
			}, 3100);

		},

		setImg(o){
			this.setData({
				url:o
			})
		},

		downTimeOp(o){
			this.setData({
				downTime:o
			})
		},

		//显示发信息内容
		showSendMsg(){
			this.setData({
				showSendMsg:!this.data.showSendMsg
			})
		},

		//给敌人发信息
		sendMsgTxt(e){

			let idx = e.target.dataset.idx;

			console.info(this.data.enemyData.enemyOpenId)


			getApp().cloud.application.httpRequest({
		
			'path' : '/api/c/game/sendTextToEnemy', 
			'method':'POST',
			'headers':{ "Content-Type":"application/json"},
			'params':{},
			'body':{
				"openId": this.data.enemyData.enemyOpenId,
  			"text": this.data.saoHua[idx].txt
			},
		
			'exts':{}  }).then(res => {

				console.info('发信息接口返回============》',res)

				this.mineMsg(this.data.saoHua[idx].txt)

			})



		},

		//获取随机位置参数
		random(num){
			return Math.round((Math.random() * (num ? num : 3)))
		},

		//初始化数据
    initData(){

      let data = JSON.parse(JSON.stringify(config))

      let game = [];
			let oppen = [];
			
			let lin = []

      if(this.props.difficulty == 'easily'){

				lin = data.six;

        oppen = data.sixEnd;
        
      }else if(this.props.difficulty == 'medium'){

				lin = data.nine;

        oppen = data.nineEnd;

      }else if(this.props.difficulty == 'senior'){
				
        lin = data.twelve;
        oppen = data.twelveEnd;

			}

				lin.map((v,i) => {
					v.pictureIndex = i+1
				})


        for(let i=0; i<4; i++){

					let rom = this.random(lin.length <= 3 ? 2 : 3)

					console.info('拿值==================>',lin[rom])

					lin[rom].x1 = this.data.locaDataRandom[i].x
					// lin[rom].y1 = this.props.difficulty == 'easily' ?  this.data.locaDataRandom[i].y : this.props.difficulty == 'medium' ? 180 * (750 / my.getSystemInfoSync().windowWidth) : 170 * (750 / my.getSystemInfoSync().windowWidth)
					lin[rom].y1 = this.data.locaDataRandom[i].y

					console.info(rom)

					game.push(lin[rom])
					lin.splice(rom,1)
					

				}



				lin.map((v,i) => {

					let rom = this.random()
					
					v.x1 = this.data.locaDataRandom[rom].x
					// v.y1 = this.props.difficulty == 'easily' ?  this.data.locaDataRandom[i].y : this.props.difficulty == 'medium' ? 180 * (750 / my.getSystemInfoSync().windowWidth) : 170 * (750 / my.getSystemInfoSync().windowWidth)

					v.y1  = this.data.locaDataRandom[rom].y

					game.push(v)

				})
			



      this.setData({
          gameData:game,
          opponent:oppen,
          bili:750 / my.getSystemInfoSync().windowWidth,
          colum:this.props.colum / 3,
          mineData:this.props.mineData,
          enemyData:this.props.enemyData,
      })



    },

    addIndex(e){
      let index = e.target.dataset.index;

      let aa = this.data.gameData;

      aa.map((v,i) => {
        if(i == index){
					v.zIndex = 3;
					
        }else{
          if(!v.disable){
            v.zIndex = 2
          }
        }
      });
      

      this.setData({
        gameData:aa
      })
      
		},
		

    changeEnd(e){
    
      let x1 = e.detail.x , y1 = e.detail.y;

      let x2 = x1 * this.data.bili, y2 = y1 * this.data.bili;

      let index = e.target.dataset.index;

      let aa = this.data.gameData;

      let lin = aa[index]
          
      // (
      //       (x2 >= lin.minX - (this.data.w / this.data.row * 0.2) && x2 <= lin.minX + (this.data.w / this.data.row * 0.2) && x2 <= lin.maxX)
      //         && 
      //       (y2 >= lin.minY - (this.data.h / this.data.colum * 0.2) && y2 <= lin.minY + (this.data.h / this.data.colum * 0.2) && y2 <= lin.maxY)
      // )
      
      // 拼图敏感度修改此处

      let percentage = 0.5    //相拼图宽高的敏感区百分比

      if(
          (
            (x2 >= lin.minX - (this.data.w / this.data.row * percentage) && x2 <= lin.minX + (this.data.w / this.data.row * percentage) && x2 <= lin.maxX)
              && 
            (y2 >= lin.minY - (this.data.h / this.data.colum * percentage) && y2 <= lin.minY + (this.data.h / this.data.colum * percentage) && y2 <= lin.maxY)
          )
      ){

        if(aa[index].x1 == lin.x2 / this.data.bili && aa[index].y1 == lin.y2 / this.data.bili){
          aa[index].x1 = lin.x2 / this.data.bili + 0.01;
          aa[index].y1 = lin.y2 / this.data.bili + 0.01;
        }else{
          aa[index].x1 = lin.x2 / this.data.bili;
          aa[index].y1 = lin.y2 / this.data.bili;
        }

        

        aa[index].zIndex = 1

        aa[index].disable = true;

        // console.info('进入条件式')
        
        this.$page.gameMsg({

          idx:index,
          x:aa[index].x,
          y:aa[index].y,
          data:this.data.gameData

        })

      }else{
				
        // aa[index].x1 = x1;
				// aa[index].y1 = y1;

				if(aa[index].x1 == x1 || aa[index].y1 == y1){
					aa[index].x1 = x1 + 0.1;
					aa[index].y1 = y1 + 0.1;
				}else{
					aa[index].x1 = x1;
					aa[index].y1 = y1;
				}
				
        if(aa[index].disable){

          aa[index].disable = false;

          this.$page.gameMsg({
            idx:index,
            x:aa[index].x,
            y:aa[index].y,
            data:aa
          })
        }

        aa[index].disable = false;

      }

      this.setData({
        gameData:aa
      })

      for(let i in aa){
        if(!aa[i].disable){
          return
        }
      }

      this.clearState();

      // my.showToast({content:'恭喜您完成'});

      this.setData({
        showImg:true
      })

      // setTimeout(() => {
      //   this.$page.setData({
      //     showGame:false
      //   })
      // },1000)
      

      

		},
		

		 pintuMove(e){

			console.info(e)

			let x1 = e.detail.x * this.data.bili;
			let y1 = e.detail.y * this.data.bili;

			console.info(y1)

			let index = e.target.dataset.index;

			let aa = this.data.gameData;


			if( y1 > 500){

				let bs = 1  -  Math.abs((y1 - 500) / 100) < 0.5 ? 0.5 : 1  -  Math.abs((y1 - 500) / 100)

					aa[index].scale = bs

					this.setData({
						gameData:aa
					})
					
			}else{

					aa[index].scale = 1 

					// this.setData({
					// 	gameData:aa
					// })
				

					// this.jieliu = setTimeout(() => {
						
					// }, 500);

			}

			// aa[index].x1 = x1
			// aa[index].y1 = y1


			// this.setData({
			// 	gameData:aa
			// })
      
      // console.info('y轴==========》',y1)


    },


		//备份...********************************************************************
    // pintuMove(e){

		// 	console.info(e)

		// 	let x1 = e.changedTouches[0].clientX;
		// 	let y1 = e.changedTouches[0].clientY * this.data.bili;

		// 	let index = e.target.dataset.index;

		// 	let aa = this.data.gameData;


		// 	if( y1 > 1200){

		// 		let bs = 1  -  Math.abs((y1 - 1200) / 100) < 0.5 ? 0.5 : 1  -  Math.abs((y1 - 1200) / 100)

		// 			aa[index].scale = bs
					

		// 	}else{

		// 			aa[index].scale = 1 


		// 	}


		// 		this.setData({
		// 			gameData:aa
		// 		})
      
    //   // console.info('y轴==========》',y1)


		// },
		//备份...********************************************************************
		

    clearState(){
      clearInterval(this.stepTime);
      clearInterval(this.gameTime);
    },

    // 显示对手进度处理函数
    enemy(o){
      let data = JSON.parse(JSON.stringify(config))

      let lin = [];

      if(this.props.difficulty == 'easily'){
        
        lin = data.sixEnd;
        
      }else if(this.props.difficulty == 'medium'){

        lin = data.nineEnd;

      }else if(this.props.difficulty == 'senior'){
        
        lin = data.twelveEnd;

      }

      // console.info(o)

      o.map((v,i) => {
        lin.map((item,idx) => {

          if(idx+1 == v.pictureIndex){
            item.end = true
          }
        })
      })

      
      this.setData({
        opponent:lin
      })
    },

    //重置
    reset(){
      this.setData({
        gameData:config.twelve,
        showImg:false
      })
    },
  },
});